package com.compass.mobile.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import com.compass.mobile.databinding.FragmentDoctorSpecialColumnBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.KEY_DEPARTMENT_ID
import com.compass.mobile.framework.constant.KEY_DOCTOR_ID
import com.compass.mobile.framework.constant.KEY_TYPE
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.models.Config
import com.compass.mobile.models.DoctorInfo
import com.compass.mobile.ui.dialog.DoctorGoodAtDialog
import com.compass.mobile.ui.viewModel.DoctorViewModel
import setUrlCircle

/**
 * Description： 医生专栏首页
 * Date:2024/6/28 10:17
 */
class DoctorSpecialColumnFragment : BaseVBFragment<FragmentDoctorSpecialColumnBinding>() {
    private val doctorViewModel: DoctorViewModel by activityViewModels()
    private var doctorId: String? = null
    private var doctorInfo: DoctorInfo? = null
    private var departmentId: String? = null
    private var comeType: String? = null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            comeType = it.getString(KEY_TYPE)
            doctorId = it.getString(KEY_DOCTOR_ID)
            departmentId = it.getString(KEY_DEPARTMENT_ID)
        }

        infoFocus()

    }

    private var mPage = 1
    override fun initData() {
        //获取数据

        doctorViewModel.doctorHomePageLiveData.observe(this) {
            it?.let {
                initDoctorInfo(it.doctor, it.config)
            }
        }


    }


    /**
     * 设置医生信息
     */
    private fun initDoctorInfo(doctorHomePage: DoctorInfo, config: Config) {
        doctorInfo = doctorHomePage
        mBinding?.imgHead?.setUrlCircle(doctorHomePage.avatar)
        mBinding?.tvDoctorName?.text = doctorHomePage.name
        mBinding?.tvPositionName?.text = doctorHomePage.positionName
        mBinding?.tvDepartments?.text = doctorHomePage.departmentName
        mBinding?.tvHospitalLevel?.text = doctorHomePage.hospitalLevel
        mBinding?.tvSubmit?.text = "${doctorHomePage.statistics.favorableRate}"
        //处方权
        if (doctorHomePage.prescriptionAllow == "1") mBinding?.tvPrescription?.visible() else mBinding?.tvPrescription?.gone()

        mBinding?.tvPracticeScope?.text =
            "执业范围: ${getPracticeScope(doctorHomePage.practiceScope)}"
        mBinding?.tvCertifiedCode?.text =
            "资格证书编号: ${getCertifiedCode(doctorHomePage.certifiedCode)}"
        mBinding?.tvMeritIllness?.text = doctorHomePage.meritIllness
        mBinding?.tvIntroduce?.text = doctorHomePage.introduce

        mBinding?.tvImageTextPrice?.text = "￥${config.txtPrice}元/次"
        mBinding?.tvPhonePrice?.text = "￥${config.phonePrice}元/次"
        mBinding?.tvVideoPrice?.text = "￥${config.videoPrice}元/次"
        //焦点请求
        mBinding?.llIndependentBilling?.requestFocus()

    }


    private fun getPracticeScope(practiceScope: String?): String {
        if (practiceScope.isNullOrEmpty()) {
            return "暂未设置"
        }
        return practiceScope
    }

    private fun getCertifiedCode(certifiedCode: String?): String {
        if (certifiedCode.isNullOrEmpty()) {
            return "暂未设置"
        }
        return certifiedCode
    }

    override fun intListener() {

        mBinding?.llIndependentBilling?.onClick {

            DoctorGoodAtDialog.Builder(requireActivity(), doctorInfo).show()
        }
    }

    /**
     * 初始化infoFocus
     */
    private fun infoFocus() {
        mBinding?.llImageText?.visible()
        mBinding?.llPhone?.visible()
        mBinding?.llVideo?.visible()
        mBinding?.clyColumn?.gone()
        FocusViewUtils.scaleView(mBinding?.llImageText, 0)
        FocusViewUtils.scaleView(mBinding?.llPhone, 0)
        FocusViewUtils.scaleView(mBinding?.llVideo, 0)
    }




}
